<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 消息通知 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 内容区 -->
		<view class="container-main">
			<message-list :show-data="messageList" :loading="messageLoading" v-if="messageList.length"></message-list>
			<empty top="28%" title="暂无相关内容~" v-else></empty>
		</view>
		<!-- 底部导航 -->
		<tab-bar></tab-bar>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import messageList from "@/pages/component/message/list.vue"
	import tabBar from "@/pages/component/tab-bar/tab-bar.vue"
	export default {
		components: {
			messageList,
			tabBar,
		},
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 消息列表
				messageList: [],
				// 分类查询参数
				page: 1,
				limit: 10,
				hasMore: false,
				// 消息列表是否加载
				messageLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				shareTitle: state => state.app.shareTitle,
				shareImage: state => state.app.shareImage,
			})
		},
		onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			this.getMessageList(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.page = 1
				this.messageLoading = true
				this.getMessageList(() => {
					this.messageLoading = false
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.messageLoading = true
			this.getMessageList(() => {
				this.messageLoading = false
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getMessageList()
			}
		},
		onShareAppMessage() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
			}
		},
		onShareTimeline() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
			}
		},
		methods: {
			// 获取消息列表
			getMessageList(fn) {
				if (!uni.getStorageSync("token")) {
					this.messageList = []
					if (fn) fn()
					return
				}
				this.$util.request("main.message.list", {
					page: this.page,
					limit: this.limit,
					type: 2,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.total / this.limit ? true : false
						this.messageList = this.page == 1 ? list : [...this.messageList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取消息列表 ', error)
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		.container-main {
			padding: 32rpx;
		}
	}
</style>